package com.junyi.basis;

import cn.hutool.core.util.NumberUtil;
import org.junit.jupiter.api.Test;

/**
 * @time: 2022/7/22 16:32
 * @version: 1.0
 * @author: junyi Xu
 * @description:
 */
public class BinaryNumber {


    @Test
    public void test() {
        // 7
        System.out.println(NumberUtil.binaryToInt("111"));
        // 1111
        System.out.println(NumberUtil.getBinaryStr(15));
    }

    @Test
    public void test2() {
        long a = 482992128;
        System.out.println(pretty(NumberUtil.getBinaryStr(a)));
        // System.out.println(NumberUtil.binaryToInt("10110111"));
        // System.out.println(pretty(NumberUtil.getBinaryStr(183L)));
    }

    @Test
    public void test3() {
        String[] ps = {"183", "11", "232", "234"};
        byte[] shiftIndex = {24, 16, 8, 0};
        long ipDst = 0;
        for (int i = 0; i < ps.length; i++) {
            int val = Integer.parseInt(ps[i]);
            ipDst |= ((long) val << shiftIndex[i]);
        }
    }



    // 每8位增加一个横杠
    public String pretty(String s) {
        StringBuilder sb = new StringBuilder();
        int count = 0;
        for (int i = s.length()-1; i >= 0; i--) {
            count++;
            sb.append(s.charAt(i));
            if (count == 8 && i != 0) {
                sb.append("-");
                count = 0;
            }
        }
        return sb.reverse().toString();
    }
}
